package com.example.lancooznbk.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.widget.CustomDialog;
import com.lancoo.reviewsystem.utils.ScreenUtils;

/**
 * Created by tangminglong on 17/9/20.
 * 弹框的工具类
 */

public class DialogHelper {

    public static final String SHOW_POPUP_DETAIL_ONE = "show_popup_one";
    public static final String SHOW_POPUP_DETAIL_TWO = "show_popup_two";
    /**
     * 清除缓存
     * @param context
     * @param listener
     */
    @Deprecated
    public static void cleanCacheDialog(Context context, DialogInterface.OnClickListener listener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("是否清除缓存？").setPositiveButton("确定",listener)
                .setNegativeButton("取消",null);
        builder.create().show();
    }

    /**
     * 清除缓存
     * @param context
     * @param listener
     */
    public static void cleanCacheDialog(Context context, String caccheSize,CustomDialog.OnCustomClickListener listener){
        SpannableString spannableString = new SpannableString("缓存大小"+caccheSize+",是否清除缓存?");
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.style0),0,4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.style1),4,4+caccheSize.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.style0),4+caccheSize.length(),spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        CustomDialog builder = new CustomDialog.Builder(context)
                .title(spannableString)
                .cancelOnTouchOutside(true)
                .negativeButton("取消",null)
                .positiveButton("确定",listener)
                .icon(R.mipmap.image_warn)
                .build();
        builder.show();



    }


    /**
     * 显示第一类提示
     * @param activity
     */
    public static void showGuideOne(final BaseActivity activity){
        if (PreferencesUtils.getBoolean(activity,SHOW_POPUP_DETAIL_ONE,true)) {
            final Dialog dialog = new Dialog(activity, R.style.ZNBK_Dialog);
            dialog.show();
            LayoutInflater inflater = LayoutInflater.from(activity);
            View viewDialog = inflater.inflate(R.layout.include_guide_view_one, null);
            FrameLayout viewContent = viewDialog.findViewById(R.id.fl_ques_guide);
            int paddingTop = activity.getStatusBarHeight() + activity.getToolBarHeight() + DensityUtils.dp2px(activity, 1);
            FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) viewContent.getLayoutParams();
            layoutParams1.setMargins(DensityUtils.dp2px(activity, 2), paddingTop, DensityUtils.dp2px(activity, 2), DensityUtils.dp2px(activity, 1));
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);
            int width = point.x;
            int height = point.y;
            //设置dialog的宽高为屏幕的宽高
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
            dialog.setContentView(viewDialog, layoutParams);

            Button BtnKnowOne = (Button) viewDialog.findViewById(R.id.btn_know_one);
            BtnKnowOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    PreferencesUtils.putBoolean(activity,SHOW_POPUP_DETAIL_ONE,false);
                }
            });
        }

    }

    /**
     * 显示第二类提示
     * @param activity
     */
    public static void showGuideTwo(final BaseActivity activity){
        if (PreferencesUtils.getBoolean(activity,SHOW_POPUP_DETAIL_TWO,true)) {
            final Dialog dialog = new Dialog(activity, R.style.ZNBK_Dialog);
            dialog.show();
            LayoutInflater inflater = LayoutInflater.from(activity);
            View viewDialog = inflater.inflate(R.layout.include_guide_view_two, null);
            FrameLayout viewContent = viewDialog.findViewById(R.id.fl_ques_guide);
            int paddingTop = activity.getStatusBarHeight() + activity.getToolBarHeight() + DensityUtils.dp2px(activity, 1);
            FrameLayout.LayoutParams layoutParams1 = (FrameLayout.LayoutParams) viewContent.getLayoutParams();
            layoutParams1.setMargins(DensityUtils.dp2px(activity, 1), paddingTop, DensityUtils.dp2px(activity, 1), DensityUtils.dp2px(activity, 1));

            int spiltOnePaddingTop = activity.getNoteHeigt() + DensityUtils.dp2px(activity, 40);
            int navigationHeight = MyUtils.isNavigationBarShow(activity) ? MyUtils.getNavigationBarHeight(activity) : 0;
            int contentHeight = ScreenUtils.getScreenHeight(activity) - spiltOnePaddingTop - paddingTop - navigationHeight;
            LinearLayout LayoutUp = (LinearLayout) viewDialog.findViewById(R.id.layout_guide_up);
            LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) LayoutUp.getLayoutParams();
            layoutParams2.height = (int) (contentHeight * 0.45 + spiltOnePaddingTop) + DensityUtils.dp2px(activity, 20);

            Display display = activity.getWindowManager().getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);
            int width = point.x;
            int height = point.y;
            //设置dialog的宽高为屏幕的宽高
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height);
            dialog.setContentView(viewDialog, layoutParams);

            Button BtnKnowTwo = (Button) viewDialog.findViewById(R.id.btn_know_two);
            BtnKnowTwo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    PreferencesUtils.putBoolean(activity,SHOW_POPUP_DETAIL_TWO,false);
                }
            });
        }

    }


}
